#include "stdafx.h"
#include "GlyphBitmap.h"


CGlyphBitmap::CGlyphBitmap(const uint8* bits, uint width, uint height, int pitch, EPixelMode pixel_mode)
: m_bits(bits)
, m_width(width)
, m_height(height)
, m_pitch(pitch)
, m_pixelMode(pixel_mode)
{
	assert(bits);
}

uint8 CGlyphBitmap::GetPixel(uint x, uint y) const
{
	assert(x < m_width && y < m_height);

	switch (m_pixelMode)
	{
	case ePixelMode_1bpp:
		return GetMonochromePixel(x, y);

	case ePixelMode_8bpp:
		return GetGrayPixel(x, y);
	}

	return 0;
}

uint8 CGlyphBitmap::GetMonochromePixel(uint x, uint y) const
{
	const uint8 BITS_PER_BYTE = 8;

	uint8 byte_base = y*m_pitch;
	uint8 byte_offset = x/BITS_PER_BYTE;

	assert(byte_offset < m_pitch);

	uint8 byte_val = m_bits[byte_base + byte_offset];
	uint8 bit_index = BITS_PER_BYTE - (x % 8) - 1;
	return (byte_val & (1 << bit_index)) ? 255 : 0;
}

uint8 CGlyphBitmap::GetGrayPixel(uint x, uint y) const
{
	return m_bits[x + y*m_width];
}